package com.oocl.pmsystem.view;

import java.util.Scanner;

import com.oocl.pmsystem.bean.Command;
import com.oocl.pmsystem.controller.CommandProcessable;
import com.oocl.pmsystem.controller.impl.PersonMsgController;
import com.oocl.pmsystem.view.delegate.PMSystemDelegate;

/**
 * View 层 ，主要： 接收命令行输入，并
 * @author androidjp
 *
 */
public class PMSystem implements PMSystemDelegate{

	private CommandProcessable commandProcessable;
	

	public PMSystem() {
		PersonMsgController controller = new PersonMsgController();
		controller.setViewDelegate(this);
		this.commandProcessable = controller;
	}

	
	
	public void start() {
		receiveCommand(null);
	}
	
	private void run() {
		System.out.print("请输入命令：");
		Scanner scan = new Scanner(System.in);
		String cmd = scan.nextLine();
		showLine();
		receiveCommand(new Command(cmd));
	}


	// /接受命令
	private void receiveCommand(Command command) {
		this.commandProcessable.dealWithCommand(command);
	}



	@Override
	public void finishProcessCommand(String output, boolean isQuit) {
		///命令完成，输出反馈结果
		if(output!=null && output.length()>0)
			System.out.println(output);
		///如果不是quit
		if(!isQuit) {
			///那么继续
			showLine();
			run();
		}
	}


	@Override
	public void error(String msg, Exception e) {
		///命令在执行过程中有异常
		System.out.println(msg);
		e.printStackTrace();
	}

	private void showLine() {
		System.out.println("==================================================");
	}


	public CommandProcessable getCommandProcessable() {
		return commandProcessable;
	}



	public void setCommandProcessable(CommandProcessable commandProcessable) {
		this.commandProcessable = commandProcessable;
	}
	
	
}
